<?php
session_start();
if ($_SESSION['user'] == "") {
    ?>
<script>
    alert('当前未登录！');
    window.location.href = '../login.html';
</script>
<?php
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新建聊天室</title>
    <link href="../css/creatRoom.css" rel="stylesheet" type="text/css">

</head>
<body>
<form id="myForm" action="../action/createRoomAction.php" method="post" enctype="multipart/form-data">
    <div id="room">
        <div class="top">
            <span>创建聊天室</span>
        </div>
        <div class="foot">
            <div class="roomName">
                <div class="roomName_left">
                    <span>房主：</span>
                </div>
                <div class="roomName_right">
                    <?=$_SESSION['user']; ?>
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="roomName">
                <div class="roomName_left">
                    <span>房间名称：</span>
                </div>
                <div class="roomName_right">
                    <input type="text" name="roomName">
                    <span id="message"></span>
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="roomId">
                <div class="roomId_left">
                    <span>房间号码：</span>
                </div>
                <div class="roomId_right">
                    <input type="text" name="roomId">
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="roomNum">
                <div class="roomNum_left">
                    <span>房间人数：</span>
                </div>
                <div class="roomNum_right">
                    <input type="radio" checked="checked" name="roomNum" value="10">10人
                    <input type="radio" name="roomNum" value="100">100人
                    <input type="radio" name="roomNum" value="200">200人
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="roomImg">
                <div class="roomImg_left">
                    <span>头像：</span>
                </div>
                <div class="roomImg_right">
                    <input id="file" type="file" name="roomImg">
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="baocun">
                <input id="submit" type="submit" value="保存">
                <input type="button" onclick="window.location.href='room.php'" value="退出">
            </div>
        </div>
    </div>
</form>
<script>
    var username='<?php echo $_SESSION['user']; ?>';
    var submitButton=document.getElementById('submit');
    var messageElement=document.getElementById('message');
    var myFormElement=document.getElementById('myForm');
    submitButton.onclick=function(e) {
        e.preventDefault();
        var roomNameUsername = document.querySelector('#myForm input[name=roomName]');
        var roomName = roomNameUsername.value;
        if (roomName == "") {
            messageElement.innerHTML = '房间名称不能为空';
            return;
        };
        var roomIdElement = document.querySelector('#myForm input[name=roomId]');
        var roomId = roomIdElement.value;
        if (roomId == "") {
            messageElement.innerHTML = '房间号码不能为空';
            return;
        };
        var roomNumElement = document.querySelector('#myForm input[name=roomNum]:checked');
        var roomNum = roomNumElement.value;
        if (roomNum == "") {
            messageElement.innerHTML = '房间人数不能为空';
            return;
        }
        var roomImgElement = document.querySelector('#myForm input[name=roomImg]');
        var roomImg = roomImgElement.value;
        if (roomImg == "") {
            messageElement.innerHTML = '头像不能为空';
            return;
        };

        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    var result = JSON.parse(this.responseText);
                    if (result.error == 1) {
                        messageElement.innerHTML = result.message;
                    } else {
                        location.href = '../html/room.php';
                    }
                } else {

                    alert("异常");
                }
            }
        };
        xhr.open(myFormElement.method, myFormElement.action);
        var data = new FormData();
        data.append('username',username);
        data.append('roomName', roomName);
        data.append('roomId', roomId);
        data.append('roomNum', roomNum);
        data.append('roomImg', roomImgElement.files[0]);
        xhr.send(data);
    }




</script>
</body>
</html>